package com.assa.ws.client.notify;

import java.net.URL;

import com.eviware.soapui.impl.WsdlInterfaceFactory;
import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.WsdlRequest;
import com.eviware.soapui.impl.wsdl.WsdlSubmit;
import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
import com.eviware.soapui.model.iface.Response;

public class NotifyClient {

	private static NotifyClient instance;
	
	private WsdlProject wsdlProject;
	private WsdlInterface wsdlInterface;
	private String wsdlDocument;
	private URL wsdlUrl;
	private WsdlRequest request;

	public NotifyClient() {
	}
	
	
	
	public NotifyClient(String documentLocation) {
		try {
			this.wsdlDocument = documentLocation;
			this.wsdlProject = new WsdlProject();
			this.wsdlInterface = WsdlInterfaceFactory.importWsdl(wsdlProject, wsdlDocument, true)[0];
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	} 
	
	public NotifyClient(URL wsdlLocation) {
		try {
			this.wsdlUrl = wsdlLocation;
			this.wsdlProject = new WsdlProject();
			wsdlInterface = WsdlInterfaceFactory.importWsdl(wsdlProject, wsdlLocation.toString(), true)[0];
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	} 
	
	public String notifyEvent(String event) throws Exception {
		WsdlOperation operation = (WsdlOperation) wsdlInterface.getOperationByName( "Notify" );
		// create a new empty request for that operation
		WsdlRequest request = operation.addNewRequest( "My request" );
		// generate the request content from the schema
		request.setRequestContent( event );
		// submit the request
		WsdlSubmit submit = (WsdlSubmit) request.submit( new WsdlSubmitContext( request ), false );
		// wait for the response
		Response response = submit.getResponse();
		
		return response.getContentAsString();
	}
	
	public void setWsdlDocument(String wsdlDocument) {
		this.wsdlDocument = wsdlDocument;
	}
	
	public void initialize() {
		try {
			this.wsdlProject = new WsdlProject();
			this.wsdlInterface = WsdlInterfaceFactory.importWsdl(wsdlProject, wsdlDocument, true)[0];
		} catch (Exception e) {
			throw new RuntimeException(e);
		}	
	}
	
}
